package com.level2app.ui.search;

import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.bean.SearchFilterChooseBean;
import com.level2app.databinding.ActivitySearchFilterBinding;
import com.level2app.ui.search.view.SearchFilterViewModel;
import com.level2app.utils.DateUtil;
import com.level2app.utils.KeyboardUtils;
import com.level2app.utils.ScreenUtil;
import com.level2app.view.dialog.DateDialog;

import java.util.Date;

/**
 * 搜索筛选页
 */
public class SearchFilterActivity extends BaseActivity<ActivitySearchFilterBinding, SearchFilterViewModel> {
    private float minValue = 0, maxValue = 1000;
    private boolean isFirst = true;
    private int chooseTag = 1;
    private Date mStartDate, mEndDate;

    @Override
    protected SearchFilterViewModel bindModel() {
        return getViewModel(this, SearchFilterViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        binding.setMViewModel(viewModel);
        binding.titleTv.setText(getString(R.string.filter_post));
        binding.titleRl.setPadding(0, ScreenUtil.getStatusBarHeight(context), 0, 0);
        binding.resetAllTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.resetAllTv.getPaint().setAntiAlias(true);
        refreshPrice();
    }

    private void refreshPrice() {
        viewModel.minData.setValue("$" + String.format("%.0f", minValue));
        viewModel.maxData.setValue("$" + String.format("%.0f", maxValue));
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.onDelayClick(binding.ivBack, o -> {
            finish();
        });
        binding.doubleSeekBar.setOnRangeListener((low, big) -> {
            //最大值big和最小值low
            minValue = low;
            maxValue = big;
            refreshPrice();
        });
        KeyboardUtils.registerSoftInputChangedListener(this, height -> {
            if (height == 0) {
                if (isFirst) {
                    isFirst = false;
                    return;
                }
                //软盘关闭
                if (!TextUtils.isEmpty(viewModel.minData.getValue())) {
                    if (viewModel.minData.getValue().startsWith("$")) {
                        minValue = Float.parseFloat(viewModel.minData.getValue().substring(1));
                    } else {
                        minValue = Float.parseFloat(viewModel.minData.getValue());
                    }
                }
                if (!TextUtils.isEmpty(viewModel.maxData.getValue())) {
                    if (viewModel.maxData.getValue().startsWith("$")) {
                        maxValue = Float.parseFloat(viewModel.maxData.getValue().substring(1));
                    } else {
                        maxValue = Float.parseFloat(viewModel.maxData.getValue());
                    }
                }
                if (minValue > maxValue) {
                    float f = minValue;
                    minValue = maxValue;
                    maxValue = f;
                }
                refreshPrice();
                binding.doubleSeekBar.setSmallAndBigValue(minValue, maxValue);
                binding.minEt.clearFocus();
                binding.maxEt.clearFocus();
            }
        });

        binding.radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.mostRelevantRb) {
                chooseTag = 1;
            } else if (i == R.id.mostRecentRb) {
                chooseTag = 2;
            } else {
                chooseTag = 3;
            }

        });

        viewModel.onDelayClick(binding.selectDateLl, o -> {
            DateDialog dateDialog = new DateDialog(context, (startDate, endDate) -> {
                mStartDate = startDate;
                mEndDate = endDate;
                binding.dateRangeTv.setText(getString(R.string.date_range, DateUtil.date2ToMonthDayEnglish(mStartDate),
                    DateUtil.date2ToMonthDayEnglish(mEndDate), DateUtil.getNowYear()));
            });
            dateDialog.show();
        });

        viewModel.onDelayClick(binding.resetAllTv, o -> {
            //重置
            chooseTag = 1;
            binding.mostRelevantRb.setChecked(true);
            binding.mostRecentRb.setChecked(false);
            binding.mostValueRb.setChecked(false);
            binding.dateRangeTv.setText(getString(R.string.select_date_range));
            mStartDate = null;
            mEndDate = null;
            minValue = 0;
            maxValue = 1000;
            binding.doubleSeekBar.setSmallAndBigValue(minValue, maxValue);
            binding.seeResultTv.setBackgroundResource(R.drawable.shape_btn_bg_def);
            refreshPrice();
        });

        viewModel.onDelayClick(binding.seeResultTv, o -> {
            setResult();
        });
    }

    //返回上一级处理
    private void setResult() {
        SearchFilterChooseBean bean = new SearchFilterChooseBean();
        if (chooseTag == 1) {
            bean.setSortString("Most Relevant");
        } else if (chooseTag == 2) {
            bean.setSortString("Most Recent");
        } else {
            bean.setSortString("Most Value");
        }
        if (mStartDate != null && mEndDate != null) {
            bean.setEndDate(mEndDate);
            bean.setStartDate(mStartDate);
            //判断是不是一个月，判断是不是一年
            if (DateUtil.getDateSameMonth(mStartDate, mEndDate)) {
                //June 14-30
                String s = DateUtil.getDateYear(mStartDate) + " " + DateUtil.date2ToMonthEnglish(mStartDate) + " " + DateUtil.getDateDay(mStartDate)
                    + "-" + DateUtil.getDateDay(mEndDate);
                bean.setDateString(s);
            } else if (DateUtil.getDateSameYear(mStartDate, mEndDate)) {
                //June 14-July 30
                String s = DateUtil.getDateYear(mStartDate) + " " + DateUtil.date2ToMonthEnglish(mStartDate) + " " + DateUtil.getDateDay(mStartDate)
                    + "-" + DateUtil.date2ToMonthEnglish(mEndDate) + " " + DateUtil.getDateDay(mEndDate);
                bean.setDateString(s);
            } else {
                //2024 June 14-2025 July 30
                String s = DateUtil.getDateYear(mStartDate) + " " + DateUtil.date2ToMonthEnglish(mStartDate) + " " + DateUtil.getDateDay(mStartDate)
                    + "-" + DateUtil.getDateYear(mEndDate) + " " + DateUtil.date2ToMonthEnglish(mEndDate) + " " + DateUtil.getDateDay(mEndDate);
                bean.setDateString(s);
            }
        }
        bean.setStartPrice(minValue);
        bean.setEndPrice(maxValue);
        String priceString = "$" + String.format("%.0f", minValue) + "-" + "$" + String.format("%.0f", maxValue);
        bean.setPriceString(priceString);
        Intent intent = new Intent();
        intent.putExtra("date", bean);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}